package controller;

import dao.UserDaoImpl;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;

import java.io.IOException;

/**
 * 简介说明: 登录验证
 *
 * @author: Half_Q_0115
 * @date: 2024/09/17 15:07:02
 * @version: 1.0
 */

@WebServlet("/login")
public class LoginServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html;charset=UTF-8");
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        UserDaoImpl userDao = new UserDaoImpl();
        if (username != null && password != null) {
            boolean ret = userDao.checkUser(username, password);
            if (ret) {
                HttpSession session = req.getSession();
                session.setAttribute("id", username);
                session.setAttribute("identify", userDao.getIdentityById(username));
                session.setAttribute("name", userDao.getNameById(username));
                session.setAttribute("college_id", userDao.getCollegeIdById(username));
                resp.getWriter().print(1);
            }else {
                resp.getWriter().print(0);
            }
        }

    }
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req, resp);
    }
}
